﻿//设计模式：策略模式

/*
* Define a family of algorithms, encapsulate each one,and make 
* them interchangeable. Strategy lets the algorithm vary 
* independently from clients that use it.
* 
* 定义一系列的算法类，将每个算法分别封装起来，让它们可以互相替换。
*/






using Microsoft.Extensions.DependencyInjection;

var services = new ServiceCollection();
services.AddScoped<IRepository, RedisRepository>();
services.AddScoped<AppService>();
var builder = services.BuildServiceProvider();

var appService = builder.GetRequiredService<AppService>();

appService.Create(null);

//依赖注入，IoC











Console.ReadKey();

public interface IRepository
{
    void Add(object entity);
}

public class EFCoreRepository : IRepository
{
    public void Add(object entity)
    {
        Console.WriteLine("EF Core 的仓储");
    }
}

public class RedisRepository : IRepository
{
    public void Add(object entity)
    {
        Console.WriteLine("Redis 仓储");
    }
}

class AppService
{
    private readonly IRepository _repository;

    public AppService(IRepository repository)
    {
        this._repository = repository;
    }

    public void Create(object entity)
    {
        _repository.Add(entity);
    }
}